###########################################
#  Primary Divisions: How Voters Evaluate Policy and Group Differences in Intra-Party Contests
#   - Forthcoming at The Journal of Politics
#   - Henderson et al 2021
#
###########################################
#  - code by S. Goggin & J. Henderson
########################################################
# This file assesses some desciptives from the affinity scores/measures for candidates using intersction of covariates and experimental items
########################################################
# inputs :: /data/data_matrix_scored.Rdata :: updated candidate and voter data

# outputs ::
# => summary_affinity_cor_items.tex
# => summary_affinity_cor_issues.tex

#dirs="~/Dropbox/replication0/"
#dirs should be set here or in runR.R 

rm(list=ls()[which(ls()!='dirs')])
library(xtable)
setwd(dirs)

# PRODUCE CORRELATIONS FOR SUMMARY TABLE
load('data/data_matrix_scored.Rdata')

# PRODUCED FOR TABLE II IN APX
varnm=as.data.frame(cbind(c(
'roll_tpp_act','roll_trd_adj','gays_policy1','roll_medicar','roll_rpl_aca','roll_minwage','immi_policy1','immi_policy2','immi_policy3','immi_policy4','immi_policy5','immi_policy6','immi_policy7','immi_policy8','abrt_policy1','abrt_policy2','abrt_policy3','abrt_policy4','abrt_policy5','abrt_policy6','roll_educrfr','roll_infrast','roll_medicar','roll_rpl_aca','roll_minwage','envs_policy1','envs_policy2','envs_policy3','envs_policy4','crme_policy1','crme_policy2','crme_policy3','crme_policy4','dfns_policy2','roll_usa_fre','roll_iransct','guns_policy1','guns_policy2','guns_policy3','guns_policy4'),
c(
cor.test(data_matrix$roll_tpp_act,data_matrix$pid)$est,cor.test(data_matrix$roll_trd_adj,data_matrix$pid)$est,
cor.test(data_matrix$gays_policy1,data_matrix$pid)$est,
cor.test(data_matrix$roll_medicar,data_matrix$pid)$est,cor.test(data_matrix$roll_rpl_aca,data_matrix$pid)$est,cor.test(data_matrix$roll_minwage,data_matrix$pid)$est,
cor.test(data_matrix$immi_policy1,data_matrix$pid)$est,cor.test(data_matrix$immi_policy2,data_matrix$pid)$est,cor.test(data_matrix$immi_policy3,data_matrix$pid)$est,cor.test(data_matrix$immi_policy4,data_matrix$pid)$est,cor.test(data_matrix$immi_policy5,data_matrix$pid)$est,cor.test(data_matrix$immi_policy6,data_matrix$pid)$est,cor.test(data_matrix$immi_policy7,data_matrix$pid)$est,cor.test(data_matrix$immi_policy8,data_matrix$pid)$est,
cor.test(data_matrix$abrt_policy1,data_matrix$pid)$est,cor.test(data_matrix$abrt_policy2,data_matrix$pid)$est,cor.test(data_matrix$abrt_policy3,data_matrix$pid)$est,cor.test(data_matrix$abrt_policy4,data_matrix$pid)$est,cor.test(data_matrix$abrt_policy5,data_matrix$pid)$est,cor.test(data_matrix$abrt_policy6,data_matrix$pid)$est,
cor.test(data_matrix$roll_educrfr,data_matrix$pid)$est,cor.test(data_matrix$roll_infrast,data_matrix$pid)$est,cor.test(data_matrix$roll_medicar,data_matrix$pid)$est,cor.test(data_matrix$roll_rpl_aca,data_matrix$pid)$est,cor.test(data_matrix$roll_minwage,data_matrix$pid)$est,
cor.test(data_matrix$envs_policy1,data_matrix$pid)$est,cor.test(data_matrix$envs_policy2,data_matrix$pid)$est,cor.test(data_matrix$envs_policy3,data_matrix$pid)$est,cor.test(data_matrix$envs_policy4,data_matrix$pid)$est,
cor.test(data_matrix$crme_policy1,data_matrix$pid)$est,cor.test(data_matrix$crme_policy2,data_matrix$pid)$est,cor.test(data_matrix$crme_policy3,data_matrix$pid)$est,cor.test(data_matrix$crme_policy4,data_matrix$pid)$est,
cor.test(data_matrix$dfns_policy2,data_matrix$pid)$est,cor.test(data_matrix$roll_usa_fre,data_matrix$pid)$est,cor.test(data_matrix$roll_iransct,data_matrix$pid)$est,
cor.test(data_matrix$guns_policy1,data_matrix$pid)$est,cor.test(data_matrix$guns_policy2,data_matrix$pid)$est,cor.test(data_matrix$guns_policy3,data_matrix$pid)$est,cor.test(data_matrix$guns_policy4,data_matrix$pid)$est)
))

varnm[,1]=as.character(varnm[,1])
varnm[,2]=as.numeric(as.character(varnm[,2]))

print(xtable(varnm,digits=3),file='appendix/figures/summary_affinity_cor_items.tex')

# PRODUCED FOR TABLE III IN APX
varnm=as.data.frame(
cbind(c('libcon_abrt','libcon_crme','libcon_need','libcon_envs','libcon_guns','libcon_envs','libcon_immi','libcon_trad','libcon_taxs','libcon_need','libcon_dfns','libcon_envs','libcon_immi','libcon_trad','libcon_taxs','libcon_need','libcon_taxs','libcon_envs','libcon_guns','libcon_abrt','libcon_gays','libcon_envs','libcon_immi','libcon_trad','libcon_taxs','libcon_need'),

c(cor.test(data_matrix$libcon_abrt,data_matrix$pid)$est,

cor.test(data_matrix$libcon_crme,data_matrix$pid)$est,
cor.test(data_matrix$libcon_need,data_matrix$pid)$est,

cor.test(data_matrix$libcon_envs,data_matrix$pid)$est,

cor.test(data_matrix$libcon_guns,data_matrix$pid)$est,

cor.test(data_matrix$libcon_envs,data_matrix$pid)$est,
cor.test(data_matrix$libcon_immi,data_matrix$pid)$est,
cor.test(data_matrix$libcon_trad,data_matrix$pid)$est,
cor.test(data_matrix$libcon_taxs,data_matrix$pid)$est,
cor.test(data_matrix$libcon_need,data_matrix$pid)$est,

cor.test(data_matrix$libcon_dfns,data_matrix$pid)$est,

cor.test(data_matrix$libcon_envs,data_matrix$pid)$est,
cor.test(data_matrix$libcon_immi,data_matrix$pid)$est,
cor.test(data_matrix$libcon_trad,data_matrix$pid)$est,
cor.test(data_matrix$libcon_taxs,data_matrix$pid)$est,
cor.test(data_matrix$libcon_need,data_matrix$pid)$est,

cor.test(data_matrix$libcon_taxs,data_matrix$pid)$est,

cor.test(data_matrix$libcon_envs,data_matrix$pid)$est,

cor.test(data_matrix$libcon_guns,data_matrix$pid)$est,

cor.test(data_matrix$libcon_abrt,data_matrix$pid)$est,
cor.test(data_matrix$libcon_gays,data_matrix$pid)$est,

cor.test(data_matrix$libcon_envs,data_matrix$pid)$est,
cor.test(data_matrix$libcon_immi,data_matrix$pid)$est,
cor.test(data_matrix$libcon_trad,data_matrix$pid)$est,
cor.test(data_matrix$libcon_taxs,data_matrix$pid)$est,
cor.test(data_matrix$libcon_need,data_matrix$pid)$est)))

varnm[,1]=as.character(varnm[,1])
varnm[,2]=as.numeric(as.character(varnm[,2]))
print(xtable(varnm,digits=3),file='appendix/figures/summary_affinity_cor_issues.tex')

#end
